package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ServiceEntity;

/**
 * Created by qiuzhenhuan on 2019/6/16.
 */

public class ADA_CartServicesSimple extends RecyclerView.Adapter<ADA_CartServicesSimple.ViewHolder> {

    private Context mContext;
    private List<ServiceEntity> datas;
    private OnItemDeleteListener listener;

    public ADA_CartServicesSimple(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ADA_CartServicesSimple.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_cart_service_simple, parent, false);
        return new ADA_CartServicesSimple.ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ADA_CartServicesSimple.ViewHolder holder, final int position) {
        if (datas == null) {
            return;
        }
        holder.tvServiceName.setText(datas.get(position).getServiceName());
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int pos = datas.get(position).getPositionInServeList();
                    datas.remove(position);
                    listener.onServiceDelete(pos);
                }
                notifyDataSetChanged();
            }
        });
    }


    public void setDatas(List<ServiceEntity> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void setOnItemDeleteListener(OnItemDeleteListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_delete)
        AppCompatImageView ivDelete;
        @BindView(R.id.tv_cart_service_name)
        TextView tvServiceName;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public List<ServiceEntity> getServiceList() {
        return datas;
    }

    public interface OnItemDeleteListener {
        void onServiceDelete(int position_in_service_list);
    }
}
